money = 1000 #初始化资金为1000元

#商品列表
goods =\
    [
        {"name":"苹果","price": 10},
        {"name":"牛奶","price": 5},
        {"name":"水杯","price": 50},
        {"name":"面包","price": 10},
        {"name":"可乐","price": 5},
        {"name":"挂面","price": 15},
        {"name":"手机","price":2000}
    ]

#空购物车
shopping_cart = []

print("=== 欢迎来到购物系统 ===")
print(f"您的余额：{money}元")
print("商品：",[g["name"]for g in goods])

while True:
    item =input("\n请输入想要购买的商品名称（Q退出）").strip()

    if item.lower() == 'q':
        break

    found_item = None
    for product in goods:
        if product["name"] == item:
            found_item = product
            break

        # 商品不存在的情况
    if not found_item:
        print("提示：没有这个商品，请选择其他商品！")
        continue

    # 商品存在时检查余额
    if money >= found_item["price"]:
        # 添加到购物车
        shopping_cart.append(found_item)
        # 扣减余额
        money -= found_item["price"]
        print(f"成功添加 {found_item['name']} 到购物车！")
        print(f"当前余额: {money}元")
    else:
        print("提示：余额不足！请到其他超市购买！")
        print(f"商品价格: {found_item['price']}元, 您的余额: {money}元")

# 5. 打印购物小票
print("\n===== 购物小票 =====")
print("商品名称\t单价")
print("-" * 30)

total_cost = 0
for item in shopping_cart:
    print(f"{item['name']}\t{item['price']}元")
    total_cost += item["price"]

print("-" * 30)
print(f"总计: {total_cost}元")
print(f"余额: {money}元")
print("感谢您的光临！")